<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Plots a graph with each edge width proportional to its weight.</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/graph_laplacian/html/plotgraph.html">
<link rel="stylesheet" href="../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Plots a graph with each edge width proportional to its weight.</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
Text output
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="keyword">function</span> plotgraph(A,xy,weights)
<span class="comment">% Edges with positive weights are drawn in blue; negative weights in red.</span>
<span class="comment">%</span>
<span class="comment">% Input parameters:</span>
<span class="comment">% A       --- incidence matrix of the graph (size is n x m)</span>
<span class="comment">%             (n is the number of nodes and m is the number of edges)</span>
<span class="comment">% xy      --- horizontal and vertical positions of the nodes (n x 2 matrix)</span>
<span class="comment">% weights --- m vector giving edge weights</span>
<span class="comment">%</span>
<span class="comment">% Original by Lin Xiao</span>
<span class="comment">% Modified by Almir Mutapcic</span>

<span class="comment">% graph size</span>
[n,m]= size(A);

<span class="comment">% set the graph scale and normalize the coordinates to lay in [-1,1] square</span>
R = max(max(abs(xy))); <span class="comment">% maximum abs value of the xy coordinates</span>
x = xy(:,1)/R; y = xy(:,2)/R;

<span class="comment">% normalize weight vector to range between +1 and -1</span>
weights = weights/max(abs(weights));

<span class="comment">% internal parameters (tune these parameters to make the plot look pretty)</span>
<span class="comment">% (note that the graph coordinates and the weights have been rescaled</span>
<span class="comment">% to a common unity scale)</span>
<span class="comment">%rNode = 0.005;     % radius of the node circles</span>
rNode = 0;          <span class="comment">% set the node radius to zero if you do not want the nodes</span>
wNode = 2;          <span class="comment">% line width of the node circles</span>
PWColor = [0 0 1];  <span class="comment">% color of the edges with positive weights</span>
NWColor = [1 0 0];  <span class="comment">% color of the edges with negative weights</span>
Wmin = 0.0001;      <span class="comment">% minimum weight value for which we draw an edge</span>
max_width = 0.05;   <span class="comment">% drawn width of edge with maximum absolute weight</span>

<span class="comment">% first draw the edges with patch widths proportional to the weights</span>
<span class="keyword">for</span> i=1:m
  <span class="keyword">if</span> ( abs(weights(i)) &gt; Wmin )
    Isrc = find( sign(weights(i))*A(:,i)&gt;0 );
    Idst = find( sign(weights(i))*A(:,i)&lt;0 );
  <span class="keyword">else</span>
    Isrc = find( A(:,i)&gt;0 );
    Idst = find( A(:,i)&lt;0 );
  <span class="keyword">end</span>

  <span class="comment">% obtain edge patch coordinates</span>
  xdelta = x(Idst) - x(Isrc); ydelta = y(Idst) - y(Isrc);
  RotAgl = atan2( ydelta, xdelta );
  xstart = x(Isrc) + rNode*cos(RotAgl); ystart = y(Isrc) + rNode*sin(RotAgl);
  xend   = x(Idst) - rNode*cos(RotAgl); yend   = y(Idst) - rNode*sin(RotAgl);
  L = sqrt( xdelta^2 + ydelta^2 ) - 2*rNode;

  <span class="keyword">if</span> ( weights(i) &gt; Wmin )
    W = abs(weights(i))*max_width;
    drawedge(xstart, ystart, RotAgl, L, W, PWColor);
    hold <span class="string">on</span>;
  <span class="keyword">elseif</span> ( weights(i) &lt; -Wmin )
    W = abs(weights(i))*max_width;
    drawedge(xstart, ystart, RotAgl, L, W, NWColor);
    hold <span class="string">on</span>;
  <span class="keyword">else</span>
    plot([xstart xend],[ystart yend],<span class="string">'k:'</span>,<span class="string">'LineWidth'</span>,2.5);
  <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="comment">% the circle to draw around each node</span>
angle = linspace(0,2*pi,100);
xbd = rNode*cos(angle);
ybd = rNode*sin(angle);

<span class="comment">% draw the nodes</span>
<span class="keyword">for</span> i=1:n
  plot( x(i)+xbd, y(i)+ybd, <span class="string">'k'</span>, <span class="string">'LineWidth'</span>, wNode );
<span class="keyword">end</span>;
axis <span class="string">equal</span>;
set(gca,<span class="string">'Visible'</span>,<span class="string">'off'</span>);
hold <span class="string">off</span>;

<span class="comment">%********************************************************************</span>
<span class="comment">% helper function to draw edges in the graph</span>
<span class="comment">%********************************************************************</span>
<span class="keyword">function</span> drawedge( x0, y0, RotAngle, L, W, color )
xp = [     0   L      L    L   L     L     0      0  ];
yp = [-0.5*W -0.5*W -0.5*W 0 0.5*W 0.5*W 0.5*W -0.5*W];
RotMat = [cos(RotAngle) -sin(RotAngle); sin(RotAngle) cos(RotAngle)];

DrawCoordinates = RotMat*[ xp; yp ];
xd = x0 + DrawCoordinates(1,:);
yd = y0 + DrawCoordinates(2,:);

<span class="comment">% draw the edge</span>
patch( xd, yd, color );
</pre>
</div>
</body>
</html>